package cn.leaf.web.controller;

import cn.leaf.common.util.DateUtil;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.util.Date;

/**
 *
 * description: 基础控制类
 *
 * @author twinkle leaf
 * @version v1.0.0
 * @since 2021-11-07 10:41:57
 * slogan: There are no two identical leaves in the world.
 *
 */
public class BaseController {

	/**
	 * 将前台传递过来的日期格式的字符串，自动转化为Date类型
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// Date 类型转换
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				setValue(DateUtil.parseLocalDateTime(text, DateUtil.DATETIME_FORMATTER));
			}
		});
	}
}
